import 'package:flutter/material.dart';
import 'package:flutter_taotaoshipin/base/base_refresh_controller.dart';
import 'package:get/get.dart';

import 'empty_view.dart';
import 'error_view.dart';
import 'loading_view.dart';

typedef RefreshWidgetCallback<T extends BaseRefreshController> = Widget
    Function(T);

class RefreshStatusView<T extends BaseRefreshController>
    extends StatelessWidget {
  const RefreshStatusView({
    Key? key,
    required this.controller,
    required this.contentBuilder,
    this.tag,
    this.background,
    this.showScrollToTop = false,
    this.emptyTips,
  }) : super(key: key);

  final T controller;
  final String? tag;
  final RefreshWidgetCallback contentBuilder;
  final Widget? background;
  final bool showScrollToTop;
  final String? emptyTips;
  get value => controller.loadStatus;

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stack(
        fit: StackFit.expand,
        children: [
          if (background != null) background!,
          GetBuilder<T>(
            tag: tag,
            builder: (controller) {
              return IndexedStack(
                index: value,
                children: [
                  const LoadingView(),
                  contentBuilder(controller),
                  EmptyView(
                      tips: emptyTips,
                      retryAction: controller.onRetry),
                  ErrorView(retryAction: controller.onRetry),
                ],
              );
            },
          ),
          if (showScrollToTop)
            Positioned(
              right: 15,
              bottom: 15,
              child: FloatingActionButton(
                onPressed: () {
                  controller.refreshController.position?.animateTo(0,
                      duration: Duration(milliseconds: 300),
                      curve: Curves.easeInOut);
                },
                child: Icon(Icons.arrow_upward_rounded),
              ),
            ),
        ],
      ),
    );
  }
}
